<!--
 * @Description: 基础告警配置
-->
<template>
  <BaseMainSlot>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :page-no="168"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="tableData"
        rowKey="baseAlarmId"
        :table-data="tableData"
        :table-columns="tableColumn"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template
          slot="operate"
          slot-scope="scoped"
          v-if="scoped.row.baseAlarmId"
        >
          <span class="table-btns-box" @click="dbHandleSee(scoped.row)">
            <i class="el-icon-view"></i>
            <span>查看</span>
          </span>
          <span class="table-btns-box" @click="edit(scoped.row)">
            <i class="el-icon-edit-outline"></i>
            <span>修改</span>
          </span>

          <span
            class="table-btns-box table_del_btn"
            @click="handleDelete(scoped.row)"
          >
            <i class="el-icon-delete"></i>
            <span>删除</span>
          </span>
        </template>
      </e-table>
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="titleText + '基础告警配置'"
        :visible.sync="showAddEditDialog"
        width="40%"
        @close="cancel('ruleForm')"
      >
        <el-form
          ref="ruleForm"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item
            v-if="nowChanceStau === 'add'"
            label="模板选择:"
            class="w50p"
          >
            <el-select
              v-model="nowalarmTemplateId"
              :disabled="mixDialogDisabled"
              clearable
              @change="chancetemple"
            >
              <el-option
                v-for="(item, index) in alarmTemplatelist"
                :key="index"
                :value="item.alarmTemplateId"
                :label="item.templateName"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备选择:" class="w50p" prop="batchNo">
            <el-select
              v-model="formData.batchNo"
              :disabled="mixDialogDisabled"
              clearable
              filterable
              placeholder="请选择设备名称"
              @change="changeEquipment"
            >
              <el-option
                v-for="(item, index) in equipmentArr"
                :key="index"
                :value="item.batchNo"
                :label="item.batchName"
              />
            </el-select>
            <!-- 没有，要改 -->
            <!-- <el-input v-model="formData.batchName" />
            <el-input v-model="formData.batchNo" />-->
          </el-form-item>
          <el-form-item label="告警名称:" class="w50p" prop="baseAlarmName">
            <el-input
              v-model="formData.baseAlarmName"
              :disabled="mixDialogDisabled"
            />
          </el-form-item>
          <el-form-item label="监测数据项:" class="w50p" prop="dataItemId">
            <el-select
              v-model="formData.dataItemId"
              :disabled="mixDialogDisabled"
              filterable
              clearable
              :placeholder="formData.dataItemName"
              @change="chandataitem"
            >
              <el-option
                v-for="(item, index) in dataitemlist"
                :key="index"
                :value="item.dataItemId"
                :label="item.originalName"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="模板名称" class="w50p">
            <el-input
              v-model="formData.templateName"
              :disabled="mixDialogDisabled"
              clearable
            />
          </el-form-item>-->
          <el-form-item label="发送间隔(分钟):" class="w50p" prop="sendingInterval">
            <el-input
              v-model="formData.sendingInterval"
              :disabled="mixDialogDisabled"
              clearable
            />
          </el-form-item>
          <el-form-item label="告警通知配置:" class="w50p" prop="noticeConfigNo">
            <el-select v-model="formData.noticeConfigNo" clearable>
              <!-- @change="tifierChange" -->
              <el-option
                v-for="(item, index) in contactmanList"
                :key="index"
                :value="item.noticeConfigNo"
                :label="item.noticeConfigName"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="滤波类型:" class="w50p" prop="filterType">
            <el-select
              v-model="formData.filterType"
              :disabled="mixDialogDisabled"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="(item, index) in filterTypeList"
                :key="index"
                :value="item.name"
                :label="item.name"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="滤波值:" class="w50p" prop="filterValue">
            <el-input
              v-model="formData.filterValue"
              :disabled="mixDialogDisabled"
              clearable
            />
          </el-form-item>
          <el-form-item label="上限列表:" class="w100p">
            <div>
              <div class="line">
                <el-input
                  v-model="basetopList.levelName"
                  :disabled="mixDialogDisabled"
                  clearable
                  placeholder="级别名称"
                />
                <el-input
                  v-model="basetopList.thresholdValue"
                  type="number"
                  :disabled="mixDialogDisabled"
                  clearable
                  placeholder="级别阈值"
                />
                <el-select
                  v-model="basetopList.alarmTypeId"
                  :disabled="mixDialogDisabled"
                >
                  <el-option
                    v-for="(item, index) in alarmTypeList"
                    :key="index"
                    :label="item.alarmTypeName"
                    :value="item.alarmTypeId"
                  />
                </el-select>
                <div
                  class="line-click"
                  @click="addtopList()"
                  v-if="
                  nowChanceStau === 'add' ||
                    nowChanceStau === 'edit'
                "
                >
                  <i class="el-icon-plus"></i>
                </div>
              </div>
              <div
                v-for="(item, index) in nowtopList"
                :key="index"
                class="line"
              >
                <el-input
                  v-model="nowtopList[index].levelName"
                  :disabled="mixDialogDisabled"
                  clearable
                  placeholder="级别名称"
                />
                <el-input
                  v-model="nowtopList[index].thresholdValue"
                  type="number"
                  :disabled="mixDialogDisabled"
                  clearable
                  placeholder="级别阈值"
                />
                <el-select v-model="nowtopList[index].alarmTypeId">
                  <el-option
                    v-for="(alarmTypeitem, alarmTypeindex) in alarmTypeList"
                    :key="alarmTypeindex"
                    :label="alarmTypeitem.alarmTypeName"
                    :value="alarmTypeitem.alarmTypeId"
                  />
                </el-select>
                <div class="line-click" @click="deltopList(index)">
                  <i class="el-icon-minus"></i>
                </div>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="下限列表:" class="w100p">
            <div>
              <div class="line">
                <el-input
                  v-model="basebottomList.levelName"
                  :disabled="mixDialogDisabled"
                  clearable
                  placeholder="级别名称"
                />
                <el-input
                  v-model="basebottomList.thresholdValue"
                  type="number"
                  :disabled="mixDialogDisabled"
                  clearable
                  placeholder="级别阈值"
                />
                <el-select
                  v-model="basebottomList.alarmTypeId"
                  :disabled="mixDialogDisabled"
                >
                  <el-option
                    v-for="(item, index) in alarmTypeList"
                    :key="index"
                    :label="item.alarmTypeName"
                    :value="item.alarmTypeId"
                  />
                </el-select>
                <div
                  class="line-click"
                  @click="addbottomList()"
                  v-if="
                  nowChanceStau === 'add' ||
                    nowChanceStau === 'edit'
                "
                >
                  <i class="el-icon-plus"></i>
                </div>
              </div>
              <div
                v-for="(item, index) in nowbottomList"
                :key="index"
                class="line"
              >
                <el-input
                  v-model="nowbottomList[index].levelName"
                  :disabled="mixDialogDisabled"
                  clearable
                  placeholder="级别名称"
                />
                <el-input
                  v-model="nowbottomList[index].thresholdValue"
                  type="number"
                  :disabled="mixDialogDisabled"
                  clearable
                  placeholder="级别阈值"
                />
                <el-select v-model="nowbottomList[index].alarmTypeId">
                  <el-option
                    v-for="(alarmTypeitem, alarmTypeindex) in alarmTypeList"
                    :key="alarmTypeindex"
                    :label="alarmTypeitem.alarmTypeName"
                    :value="alarmTypeitem.alarmTypeId"
                  />
                </el-select>
                <div class="line-click" @click="delbottomList(index)">
                  <i class="el-icon-minus"></i>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel">取 消</el-button>
          <el-button
            v-show="mixDialogDisabled===false"
            class="zh-button-two"
            type="primary"
            @click="save('ruleForm')"
          >保 存</el-button>
          <el-button
            v-show="mixDialogDisabled===true"
            class="zh-button-two"
            type="primary"
            @click="closeDialog"
          >确 认</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import nowpagereq from "@/api/monitoring-alarm/basic-alarm-configuration";
import userreq from "@/api/user/index";
import contactnot from "@/api/monitoring-alarm/system-notification-configuration";
import equipScrapRecordApi from "@/api/equip-manage/equip-scrap-record";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";
export default {
  name: "BasicAlarmConfiguration",
  components: {
    ActionBar,
    BaseMainSlot,
  },
  mixins: [mymixin],
  data() {
    return {
      topStyle: "7px",
      state: 0,
      tableColumn: [
        {
          param: "batchName",
          label: "设备名称",
          minWidth: "300",
          show: true,
          notSearch: true,
        },
        {
          param: "baseAlarmName",
          label: "告警名称",
          minWidth: "300",
          show: true,
          notSearch: true,
        },
        {
          param: "dataItemName",
          label: "告警数据项",
          minWidth: "150",
          show: true,
          notSearch: true,
        },
        {
          param: "dataTypeName",
          label: "告警数据类型",
          minWidth: "150",
          show: true,
          notSearch: true,
        },
        {
          param: "dataItemName",
          label: "监测数据项",
          minWidth: "150",
          show: true,
          notSearch: true,
        },
      ],
      // 表单事件
      tableOption: {
        label: "操作",
        options: [
          {
            label: "操作",
            slotName: "operate",
          },
        ],
      },
      rules: {
        baseAlarmName: [
          {
            required: true,
            message: "请输入告警名称",
            trigger: ["change", "blur"],
          },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        sendingInterval: [
          {
            required: true,
            message: "请输入发送间隔",
            trigger: ["change", "blur"],
          },
        ],
        dataItemId: [
          {
            required: true,
            message: "请选择监测数据项目",
            trigger: ["change", "blur"],
          },
        ],
        noticeConfigNo: [
          {
            required: true,
            message: "请选择告警通知配置",
            trigger: ["change", "blur"],
          },
        ],
        filterType: [
          {
            required: true,
            message: "请选择滤波类型",
            trigger: ["change", "blur"],
          },
        ],
        filterValue: [
          {
            required: true,
            message: "请输入滤波值",
            trigger: ["change", "blur"],
          },
        ],
        batchNo: [
          {
            required: true,
            message: "请选择设备",
            trigger: ["change", "blur"],
          },
        ],
      },
      // 数值告警时
      // 数据项
      dataitemtwoindex: "",
      dataitemtwoname: "",
      dataitemtwolist: [],
      // 设施
      facilitytwoIndex: "",
      facilitytwoname: "",
      facilitylist: [],
      // 选择类型
      // selectbutlist: [
      //   { name: '基础告警配置' },
      //   { name: '告警类别管理' },
      //   { name: '告警模板配置' }
      // ],
      dataitemlist: [],
      // 告警模板
      nowalarmTemplateId: "",
      alarmTemplatelist: [],
      // 批次
      batchList: [],
      equipmentArr: [],
      nowbatch: {},
      // 主要联系人
      zhuyaocontactmanId: "",
      checkedGroupone: [],
      // 其他联系人
      qitacontactmanId: "",
      checkedGrouptwo: [],
      // 联系人可选择列表
      contactmanList: [],
      // 联系方式可选择列表
      contactmetList: [
        {
          name: "手机",
        },
        {
          name: "邮箱",
        },
        {
          name: "微信",
        },
      ],
      // 上限列表
      basetopList: {
        levelName: "",
        alarmTypeId: "",
        thresholdType: "max",
        thresholdValue: "",
      },
      nowtopList: [],
      // 下限列表
      basebottomList: {
        alarmTypeId: "",
        levelName: "",
        thresholdType: "min",
        thresholdValue: "",
      },
      nowbottomList: [],
      filterTypeList: [
        {
          name: "时间",
        },
        {
          name: "次数",
        },
      ],
      // 数据类别
      // nowdataType: {},
      // dataTypeList: [],
      // 告警类型下拉数据
      alarmTypeList: [],
      // 搜索内容
      searchList: {},
      // 选中的数据项数组 alarmContactId组成 (此数组最多为一个)
      selectIdList: [],
      dialogTitle: "",
      canInput: false,
      formData: {
        baseAlarmName: "",
        sendingInterval: "",
        filterValue: "",
        noticeConfigNo: "",
        batchName: "",
        batchNo: "",
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  created() {
    // 获取表格数据
    this.queryPageList();
  },
  mounted() {
    this.getselectlist();
  },
  methods: {
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    handleAdd() {
      // 上限列表和下限列表清空
      this.basetopList = {
        levelName: "",
        alarmTypeId: "",
        thresholdType: "max",
        thresholdValue: "",
      };
      this.nowtopList = [];
      this.basebottomList = {
        levelName: "",
        alarmTypeId: "",
        thresholdType: "min",
        thresholdValue: "",
      };
      this.nowbottomList = [];
    },
    cancel() {
      this.formData = {};
      this.basetopList = {};
      this.basebottomList = {};
      this.nowtopList = [];
      this.nowbottomList = [];
      this.showAddEditDialog = false;
      this.addActive = true;
      this.delActive = false;
      this.nowbottomList = [];
      this.$refs["ruleForm"]?.clearValidate();
      this.$refs["ruleForm"]?.resetFields();
    },
    changeTableSort(column) {
      this.sortList = column.order
        ? [
            {
              property: column.prop,
              direction: column.order == "ascending" ? "asc" : "desc",
            },
          ]
        : [];
      this.queryPageList();
    },
    //选择设备
    changeEquipment(e) {
      this.equipmentArr.forEach((el) => {
        if (el.batchNo === e) {
          this.formData["batchName"] = el.batchName;
        }
      });
    },
    // 选择模板
    chancetemple(e) {
      nowpagereq.getalarmTemplatemessagelist(e).then((res) => {
        let row = res.data;
        if (this.nowalarmTemplateId > 0) {
          this.chancetemplelist(row);
        }
      });
    },
    // 选择数据项
    chandataitem(i) {
      this.dataitemlist.forEach((el) => {
        if (el.dataItemId === i) {
          this.formData["dataItemName"] = el.dataItemName;
          this.formData["dataItemId"] = el.dataItemId;
          this.formData["dataTypeId"] = el.dataTypeId;
          this.formData["dataTypeName"] = el.dataTypeName;
          if (el.enumListType) {
            let list = el.enumListType.split(";");
            list = list.filter((ele) => {
              return ele && ele.trim();
            });
            let v = list[list.length - 1].split(":");
            this.basetopList.thresholdValue = v[0];
          }
        }
      });
    },
    // 选择模板时的数据处理
    chancetemplelist(zhrow) {
      console.log(zhrow);
      this.formData = {};
      let row = JSON.parse(JSON.stringify(zhrow));
      if (row.alarmTemplateId) {
        this.formData = {
          alarmTemplateId: row.alarmTemplateId,
          templateName: row.templateName,
          sendingInterval: row.sendingInterval,
          filterValue: row.filterValue,
          filterType: row.filterType,
          noticeConfigNo: row.noticeConfigNo,
          // dataTypeId: row.dataTypeId,
          // dataTypeName: row.dataTypeName
        };
        nowpagereq.getalarmTypelist(row.dataTypeId).then((res) => {
          let data = res.data;
          this.chancealarmtype(data);
        });
        // this.nowdataType = {
        //   value: row.dataTypeId,
        //   name: row.dataTypeName
        // }
        row.alarmTemplateThresholdList.forEach((item) => {
          if (item.charge === true) {
            this.zhuyaocontactmanId = item.alarmContactId;
            // 联系方式的赋值
            this.checkedGroupone = [];
            if (item.sendPhone === true) {
              this.checkedGroupone.push(0);
            }
            if (item.sendEmail === true) {
              this.checkedGroupone.push(1);
            }
            if (item.sendWeChat === true) {
              this.checkedGroupone.push(2);
            }
          }
          if (item.charge === false) {
            this.qitacontactmanId = item.alarmContactId;
            // 联系方式的赋值
            this.checkedGrouptwo = [];
            if (item.sendPhone === true) {
              this.checkedGrouptwo.push(0);
            }
            if (item.sendEmail === true) {
              this.checkedGrouptwo.push(1);
            }
            if (item.sendWeChat === true) {
              this.checkedGrouptwo.push(2);
            }
          }
        });
        let mainlist = row.alarmTemplateThresholdList;
        // 上限列表和下限列表的赋值
        this.basetopList = {
          levelName: "",
          alarmTypeId: "",
          thresholdType: "max",
          thresholdValue: "",
        };
        this.nowtopList = [];
        this.basebottomList = {
          levelName: "",
          alarmTypeId: "",
          thresholdType: "min",
          thresholdValue: "",
        };
        this.nowbottomList = [];
        for (var i = 0; i < mainlist.length; i++) {
          if (mainlist[i].thresholdType === "max") {
            if (this.basetopList.levelName.length === 0) {
              this.basetopList = mainlist[i];
            } else {
              this.nowtopList.push(mainlist[i]);
            }
          } else if (mainlist[i].thresholdType === "min") {
            if (this.basebottomList.levelName.length === 0) {
              this.basebottomList = mainlist[i];
            } else {
              this.nowbottomList.push(mainlist[i]);
            }
          }
        }
      }
    },
    // 添加一条上限数据
    addtopList() {
      let addvalue = {
        alarmTypeId: "",
        levelName: "",
        thresholdType: "max",
        thresholdValue: "",
      };
      this.nowtopList.push(addvalue);
    },
    // 删除一条上限数据
    deltopList(i) {
      this.nowtopList.splice(i, 1);
    },
    // 添加一条下限数据
    addbottomList() {
      let addvalue = {
        alarmTypeId: "",
        levelName: "",
        thresholdType: "min",
        thresholdValue: "",
      };
      this.nowbottomList.push(addvalue);
    },
    // 添加一条下限数据
    delbottomList(i) {
      this.nowbottomList.splice(i, 1);
    },
    // 查看
    dbHandleSee(row) {
      this.edit(row);
      this.mixDialogDisabled = true;
    },
    // 获取下拉数据
    getselectlist() {
      // 获取设施下拉数据
      let faciparams = {};
      if (this.nowChanceStau === "add") {
        faciparams = {
          organizationList: sessionStorage
            .getItem("organizationList")
            .split(","),
          farmingTypeNo: sessionStorage.getItem("farmingTypeNo"),
          facilityStatus: 1,
        };
      } else {
        faciparams = {
          organizationList: sessionStorage
            .getItem("organizationList")
            .split(","),
          farmingTypeNo: sessionStorage.getItem("farmingTypeNo"),
        };
      }
      userreq.getfacilityAll(faciparams).then((res) => {
        this.facilitylist = res.data;
      });
      this.queryEquipment();
      // // 获取批次列表 ---组织结构id跟着批次id
      // nowpagereq.getbatchlist({ simpleData: true }).then((res) => {
      //   this.batchList = res.data;
      // });
      // 获取可选择模板
      nowpagereq.getalarmTemplatelist().then((res) => {
        this.alarmTemplatelist = res.data;
        this.chancetemple(this.alarmTemplatelist[0].alarmTemplateId);
      });
      // 获取所有监测数据项
      nowpagereq.getdataitemlist().then((res) => {
        this.dataitemlist = res.data;
      });
      // 获取所有联系人
      contactnot
        .querybycondition({ organizationNo: sessionStorage.organizationNo })
        .then((res) => {
          this.contactmanList = res.data;
        });
    },
    // 条件查询设备
    queryEquipment() {
      this.equipmentArr = [];
      equipScrapRecordApi.queryEquipment({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          for (var i in data) {
            this.equipmentArr.push({
              batchName: data[i],
              batchNo: i,
            });
          }
        }
      });
    },
    // 通过设施id获取数据dataitem
    getbyconditiondataitem(par) {
      nowpagereq.getdataitemlist(par).then((res) => {
        this.dataitemtwolist = res.data;
      });
    },
    // 将选择的数据类别添加到表单中 --获取告警类型列表
    addDataType(e) {
      if (this.nowChanceStau === "add" || this.nowChanceStau === "edit") {
        this.formData["dataTypeId"] = e.value;
        this.formData["dataTypeName"] = e.name;
      }
      nowpagereq.getalarmTypelist(e.value).then((res) => {
        let data = res.data;
        this.chancealarmtype(data);
      });
    },
    // 处理数据类型数据
    chancealarmtype(list) {
      let newlist = [];
      list.forEach((item, index) => {
        newlist.push(item.alarmType);
      });
      this.alarmTypeList = newlist;
      //   "data": [
      // {
      //   "alarmType": {
      //     "alarmTypeId": 1,
      //     "alarmTypeName": "高温告警",
      //     "organizationNo": 0,
      //     "system": "SC"
      //   },
      //   "alarmTypeDataTypeMapperId": 1,
      //   "alarmTypeId": 1,
      //   "dataTypeId": 1,
      //   "dataTypeName": "温度",
      //   "organizationNo": 1
      // }
      // ],
    },
    // 添加滤波类型
    addFilterType(e) {
      if (this.nowChanceStau === "add" || this.nowChanceStau === "edit") {
        this.formData.filterType = e.name;
      }
    },
    // 获取表格数据
    queryPageList(e) {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        sortList: this.sortList || [],
        ...e,
      };
      nowpagereq.getlist(params).then((res) => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("查询失败");
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet();
        this.resetActionBar();
      });
    },

    search() {
      this.currentPage = 1;
      this.queryPageList();
    },

    handleEdit(zhrow) {
      this.formData = {};
      let row = JSON.parse(JSON.stringify(zhrow));
      console.log("handleEdit ", this.showAddEditDialog);
      // 下面时物联自动告警
      if (row.baseAlarmId) {
        this.formData = row;
        // this.formData = {
        //   baseAlarmId: row.baseAlarmId,
        //   baseAlarmName: row.baseAlarmName,
        //   sendingInterval: row.sendingInterval,
        //   filterValue: row.filterValue,
        //   filterType: row.filterType,
        //   dataItemName: row.dataItemName,
        //   dataItemId: row.dataItemId,
        //   dataTypeName: row.dataTypeName,
        //   dataTypeId: row.dataTypeId,
        //   batchNo: row.batchNo,
        //   batchName: row.batchName,
        //   nowalarmTemplateId: row.nowalarmTemplateId,
        //   noticeConfigNo: row.noticeConfigNo,
        // };
        this.nowalarmTemplateId = row.nowalarmTemplateId;

        // this.nowdataType = {
        //   value: row.dataTypeId,
        //   name: row.dataTypeName
        // }
        this.addDataType({ name: row.dataTypeName, value: row.dataTypeId });
        let mainlist = row.baseAlarmThresholdList;
        // 上限列表和下限列表的赋值
        this.basetopList = {
          levelName: "",
          alarmTypeId: "",
          thresholdType: "max",
          thresholdValue: "",
        };
        this.nowtopList = [];
        this.basebottomList = {
          levelName: "",
          alarmTypeId: "",
          thresholdType: "min",
          thresholdValue: "",
        };
        this.nowbottomList = [];
        for (var i = 0; i < mainlist.length; i++) {
          if (mainlist[i].thresholdType === "max") {
            if (this.basetopList.levelName.length === 0) {
              this.basetopList = mainlist[i];
            } else {
              this.nowtopList.push(mainlist[i]);
            }
          } else if (mainlist[i].thresholdType === "min") {
            if (this.basebottomList.levelName.length === 0) {
              this.basebottomList = mainlist[i];
            } else {
              this.nowbottomList.push(mainlist[i]);
            }
          }
        }
      }
      // this.openDialog('edit');
    },
    handleSelectionChange(list) {
      this.selectIdList = [];
      let notvalue = true;
      this.selectIdList = list.map((item) => {
        if (item.baseAlarmId) {
          return item.baseAlarmId;
        } else {
          notvalue = false;
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        return;
      }
      if (this.selectIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      nowpagereq.dellist(this.selectIdList).then((res) => {
        if (res.code === 1) {
          this.$message.success("删除成功");
          this.queryPageList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleDelete(data) {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          nowpagereq.dellist([data.baseAlarmId]).then((res) => {
            if (res.code === 1) {
              this.$message.success("删除成功");
              this.queryPageList();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message("取消删除");
        });
    },
    save() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            let editparams = {
              // baseAlarmSendMapperList: [
              //   // 主要联系人
              //   {
              //     alarmContactId: this.zhuyaocontactmanId,
              //     alarmCharge: true,
              //     sendPhone: false,
              //     sendEmail: false,
              //     sendWeChat: false
              //   },
              //   // 其他联系人
              //   {
              //     alarmContactId: this.qitacontactmanId,
              //     alarmCharge: false,
              //     sendPhone: false,
              //     sendEmail: false,
              //     sendWeChat: false
              //   }
              // ],
              baseAlarmThresholdList: [
                // 上限数据
                this.basetopList,
                ...this.nowtopList,
                // 下限数据
                this.basebottomList,
                ...this.nowbottomList,
              ],
              // 下面时一些与其他数据
              // filterType: this.formData.name,
              // dataTypeName: this.nowdataType.name,
              // dataTypeId: this.nowdataType.value,
              ...this.formData,
            };
            // if (this.nowdataType.length > 0) {
            //   this.formData.dataTypeName = this.nowdataType.name
            //   this.formData.dataTypeId = this.nowdataType.value
            // }
            // editparams
            // 联系方式
            if (this.checkedGroupone.length > 0) {
              if (this.checkedGroupone.indexOf(0) > -1) {
                editparams.baseAlarmSendMapperList[0].sendPhone = true;
              }
              if (this.checkedGroupone.indexOf(1) > -1) {
                editparams.baseAlarmSendMapperList[0].sendEmail = true;
              }
              if (this.checkedGroupone.indexOf(2) > -1) {
                editparams.baseAlarmSendMapperList[0].sendWeChat = true;
              }
            }
            if (this.checkedGrouptwo.length > 0) {
              if (this.checkedGrouptwo.indexOf(0) > -1) {
                editparams.baseAlarmSendMapperList[1].sendPhone = true;
              }
              if (this.checkedGrouptwo.indexOf(1) > -1) {
                editparams.baseAlarmSendMapperList[1].sendEmail = true;
              }
              if (this.checkedGrouptwo.indexOf(2) > -1) {
                editparams.baseAlarmSendMapperList[1].sendWeChat = true;
              }
            }
            // 编辑接口
            nowpagereq.editlist(editparams).then((res) => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.closeDialog();
                this.queryPageList();
              } else {
                this.$message.error("修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            let addparams = {
              // baseAlarmSendMapperList: [
              //   // 主要联系人
              //   {
              //     alarmContactId: this.zhuyaocontactmanId,
              //     alarmCharge: true,
              //     sendPhone: false,
              //     sendEmail: false,
              //     sendWeChat: false
              //   },
              //   // 其他联系人
              //   {
              //     alarmContactId: this.qitacontactmanId,
              //     alarmCharge: false,
              //     sendPhone: false,
              //     sendEmail: false,
              //     sendWeChat: false
              //   }
              // ],
              baseAlarmThresholdList: [
                // 上限数据
                this.basetopList,
                ...this.nowtopList,
                // 下限数据
                this.basebottomList,
                ...this.nowbottomList,
              ],
              alarmCharge: "", // 告警联系人名字
              // 下面时一些与其他数据
              // dataTypeName: this.nowdataType.name,
              // dataTypeId: this.nowdataType.value,
              ...this.formData,
            };
            // // 通过id查告警联系人名字
            // nowpagereq.getalarmCharge(this.zhuyaocontactmanId).then(
            //   res => {
            //     addparams.alarmCharge = res.data.alarmContactName
            //   }
            // )
            // ---
            // if (this.nowdataType.length > 0) {
            //   this.formData.dataTypeName = this.nowdataType.name
            //   this.formData.dataTypeId = this.nowdataType.value
            // }
            // 联系方式
            if (this.checkedGroupone.length > 0) {
              if (this.checkedGroupone.indexOf(0) > -1) {
                addparams.baseAlarmSendMapperList[0].sendPhone = true;
              }
              if (this.checkedGroupone.indexOf(1) > -1) {
                addparams.baseAlarmSendMapperList[0].sendEmail = true;
              }
              if (this.checkedGroupone.indexOf(2) > -1) {
                addparams.baseAlarmSendMapperList[0].sendWeChat = true;
              }
            }
            if (this.checkedGrouptwo.length > 0) {
              if (this.checkedGrouptwo.indexOf(0) > -1) {
                addparams.baseAlarmSendMapperList[1].sendPhone = true;
              }
              if (this.checkedGrouptwo.indexOf(1) > -1) {
                addparams.baseAlarmSendMapperList[1].sendEmail = true;
              }
              if (this.checkedGrouptwo.indexOf(2) > -1) {
                addparams.baseAlarmSendMapperList[1].sendWeChat = true;
              }
            }
            // ---
            // console.log(addparams)
            // 需要删除
            addparams.organizationNo = sessionStorage.organizationNo;
            this.batchList.forEach((el) => {
              if (el.batchNo === addparams.batchNo) {
                addparams["batchName"] = el.batchName;
              }
            });
            // console.log(addparams); return
            setTimeout(() => {
              if (!addparams.filterType) addparams.filterType = "";
              nowpagereq.addlist(addparams).then((res) => {
                if (res.code === 1) {
                  this.$message.success("新增成功！");
                  this.closeDialog();
                  this.queryPageList();
                } else {
                  this.$message.error(res.msg || "新增失败！");
                }
              });
            }, 200);
          }
        } else {
          return false;
        }
      });
    },
    openDialog(type) {
      this.showAddEditDialog = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogTitle = null;
      this.nowChanceStau = "";
      this.formData = {};
      this.nowbatch = "";
      this.basetopList = {
        levelName: "",
        alarmTypeId: "",
        thresholdType: "max",
        thresholdValue: "",
      };
      this.basebottomList = {
        alarmTypeId: "",
        levelName: "",
        thresholdType: "min",
        thresholdValue: "",
      };
      this.nowalarmTemplateId = "";
      this.nowbatch = "";
      this.nowalarmTemplateId = "";
      this.$refs["ruleForm"]?.clearValidate();
      this.$refs["ruleForm"]?.resetFields();
      this.showAddEditDialog = false;
    },
  },
};
</script>

<style scoped lang="scss">
.line {
  width: 100%;
  display: flex;
  flex-wrap: nowrap;
  .line-click {
    width: 90px;
    height: 30px;
    // background-color: aqua;
    display: flex;
    justify-content: center;
    align-items: center;
    border: snow 1px solid;
    border-radius: 50%;
    margin-bottom: 10px;
    margin-top: 5px;
    margin-left: 5px;
    cursor: pointer;
  }
}
.selectbut {
  position: absolute;
  // top: 85px;
  top: 10px;
  z-index: 99;
}
</style>
